/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Oct 2, 2009

package com.sceneworker.util.jme.coderep;

import com.acarter.scenemonitor.propertydescriptor.definitions.SpatialPropertyPage;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.Spatial.NormalsMode;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.state.RenderState;
import com.sceneworker.util.jme.coderep.JMECodeRep.ICodeRep;
import com.sun.org.apache.bcel.internal.generic.NEW;

abstract	public class SpatialCodeRep implements ICodeRep {

	public String finish(JMECodeRep cl_rep, Object cl_obj) {
		String 	lstr_cur = cl_rep.getVariableStack().pop();
		
		try {
			String	lstr_parent = cl_rep.getVariableStack().peek();
			
			if(lstr_parent != null) {
				return "\n" + lstr_parent + ".attachChild(" + lstr_cur + ");\n\n";
			}
		} catch (Exception e) {
		}
		
		return "";
	}

	public String populate(JMECodeRep cl_rep, Object cl_obj) {
		Spatial		lcl_s = (Spatial)cl_obj;
		String		lstr_temp = "";
		
		String		lstr_varName = cl_rep.getVariableStack().peek();
		if(!lcl_s.getLocalTranslation().equals(Vector3f.ZERO)) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setLocalTranslation", 
					lcl_s.getLocalTranslation().x + "f", 
					lcl_s.getLocalTranslation().y + "f", 
					lcl_s.getLocalTranslation().z + "f");
		}
		
		if(!lcl_s.getLocalScale().equals(Vector3f.UNIT_XYZ)) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setLocalScale", 
					lcl_s.getLocalScale().x + "f", 
					lcl_s.getLocalScale().y + "f", 
					lcl_s.getLocalScale().z + "f");
		}
		
		if(!lcl_s.getLocalRotation().equals(new Quaternion())) {
			lstr_temp += JMECodeRep.construct(lstr_varName + "Rotation", Quaternion.class.getName(), lcl_s.getLocalRotation().x + "f", 
					lcl_s.getLocalRotation().y + "f", 
					lcl_s.getLocalRotation().z + "f",
					lcl_s.getLocalRotation().w + "f");
			lstr_temp += JMECodeRep.method(lstr_varName, "setLocalRotation", lstr_varName + "Rotation"); 				
		}
		
		if(lcl_s.getWorldBound() != null) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setModelBound", "new " + lcl_s.getWorldBound().getClass().getName() + "()");
		}
		
		if((lcl_s.getCullHint() != CullHint.Inherit) && (lcl_s.getCullHint() != CullHint.Dynamic)) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setCullHint", "Spatial.CullHint." + lcl_s.getCullHint().name());
		}
		
		if((lcl_s.getLightCombineMode() != LightCombineMode.Inherit) && (lcl_s.getLightCombineMode() != LightCombineMode.CombineFirst)) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setLightCombineMode", "Spatial.LightCombineMode." + lcl_s.getLightCombineMode().name());
		}
		
		if((lcl_s.getTextureCombineMode() != TextureCombineMode.Inherit) && (lcl_s.getTextureCombineMode() != TextureCombineMode.CombineClosest)) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setTextureCombineMode", "Spatial.TextureCombineMode." + lcl_s.getTextureCombineMode().name());
		}
		
		if((lcl_s.getNormalsMode() != NormalsMode.Inherit) && (lcl_s.getNormalsMode() != NormalsMode.NormalizeIfScaled)) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setNormalsMode", "Spatial.NormalsMode." + lcl_s.getNormalsMode().name());
		}
		
		if(!lcl_s.isCollidable()) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setIsCollidable", "false");
		}
		
		if(lcl_s.getZOrder() != 0) {
			lstr_temp += JMECodeRep.method(lstr_varName, "getZOrder", "" + lcl_s.getZOrder());
		}
		
		if((lcl_s.getRenderQueueMode() != SpatialPropertyPage.E_RenderQueueMode.QUEUE_INHERIT.getType()) && 
				(lcl_s.getRenderQueueMode() != SpatialPropertyPage.E_RenderQueueMode.QUEUE_SKIP.getType())) {
			lstr_temp += JMECodeRep.method(lstr_varName, "setRenderQueueMode", Renderer.class.getName() + "." + SpatialPropertyPage.E_RenderQueueMode.getConstant(lcl_s.getRenderQueueMode()));
		}
		
		lstr_temp += handleRenderStates(cl_rep, lcl_s);
		
		return lstr_temp;
	}

	private	String	handleRenderStates(JMECodeRep cl_rep, Spatial cl_s) {
		String 	lstr_temp = "";
		
		for(RenderState.StateType lcl_stateType : RenderState.StateType.values()) {
			RenderState		lcl_state = cl_s.getRenderState(lcl_stateType);
			
			if(lcl_state != null) {
				lstr_temp += cl_rep.process(lcl_state);
			}
		}
		
		return lstr_temp;
	}
}
